package com.pzb.web.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.order.domain.OdOrderSetting;
import com.pzb.order.service.IOdOrderSettingService;

/**
 * 订单设置Controller
 * 
 * @author Mr_Wu
 * @date 2021-12-29
 */
@RestController
@RequestMapping("/order/orderSetting")
public class OdOrderSettingController extends BaseController
{
    @Autowired
    private IOdOrderSettingService odOrderSettingService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 获取订单设置详细信息
     */
    @GetMapping
    public AjaxResult getInfo()
    {
        OdOrderSetting orderSetting = odOrderSettingService.selectOdOrderSetting(wechatAppletService.getExtAppid());
        if(orderSetting!=null){
            orderSetting.setRegion(JSON.parseArray(orderSetting.getAreaCode()));
        }
        return AjaxResult.success(orderSetting);
    }

    /**
     * 修改订单设置
     */
    @Log(title = "订单设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OdOrderSetting odOrderSetting)
    {
        odOrderSetting.setAreaCode(odOrderSetting.getRegion().toString());
        String fullAddress = "";
        for (Object address : odOrderSetting.getRegion()) {
            JSONObject a = (JSONObject) JSONObject.toJSON(address);
            fullAddress+= a.getString("label");
        }
        odOrderSetting.setFullAddress(fullAddress+odOrderSetting.getAddress());
        return toAjax(odOrderSettingService.updateOdOrderSetting(odOrderSetting));
    }

}
